package com.ats.developer.components
{
	import com.ats.developer.components.borderpint.BBorderPoint;
	import com.ats.developer.components.borderpint.BRBorderPoint;
	import com.ats.developer.components.borderpint.BorderPoint;
	import com.ats.developer.components.borderpint.LBBorderPoint;
	import com.ats.developer.components.borderpint.LBorderPoint;
	import com.ats.developer.components.borderpint.RBorderPoint;
	import com.ats.developer.components.borderpint.TBorderPoint;
	import com.ats.developer.components.borderpint.TLBorderPoint;
	import com.ats.developer.components.borderpint.TRBorderPoint;
	import com.ats.developer.components.field.ATSImg;
	import com.ats.developer.components.field.ATSLabel;
	import com.ats.developer.model.FormCanvasModel;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	
	import flexlib.containers.DockableToolBar;
	import flexlib.containers.DragScrollingCanvas;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Box;
	import mx.containers.Canvas;
	import mx.containers.VBox;
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.controls.Text;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import mx.managers.CursorManager;
	
	public class FormDesignCanvas extends DragScrollingCanvas
	{
		[Bindable]
		public var items:ArrayCollection=new ArrayCollection;
		
		public var toolbar:DockableToolBar=new DockableToolBar();
		
		public var formCanvas:Canvas=new Canvas();
		
		public var backgroundImg:Image=new Image();
		
		public var box:Box=new Box();
		
		public var formCanvasModel:FormCanvasModel=new FormCanvasModel();
		
		public function FormDesignCanvas()
		{
			super();
			formCanvas.horizontalScrollPolicy="off";
			formCanvas.verticalScrollPolicy="off";
			formCanvas.setStyle("backgroundColor","#F0F0F0");
			formCanvas.setStyle("backgroundAlpha","0.0");
			formCanvas.addEventListener(MouseEvent.MOUSE_OVER,formCanvas_mouseOverHandler);
			formCanvas.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
			this.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandle);
			this.addEventListener(MouseEvent.CLICK,clickHandler);
			box.setStyle("backgroundAlpha","0.3");
			box.setStyle("backgroundColor","#0066FF");
			box.visible=false;
		}
		override protected function createChildren():void{
			super.createChildren();
			backgroundImg.addEventListener(Event.COMPLETE,backGroundImg_Complete);
//			backgroundImg.setStyle("verticalCenter","0");
//			backgroundImg.setStyle("horizontalCenter","0");
			formCanvas.addChild(box);
			this.addChild(backgroundImg);
			this.addChild(formCanvas);
		}
		
		public var real_width:Number=0;
		public var real_height:Number=0;
		protected function backGroundImg_Complete(event:Event):void{
			//原始图片的大小
//			this.parentDocument.designBox.scaleX=1;
//			this.parentDocument.designBox.scaleY=1;
			
			real_width = backgroundImg.contentWidth;
			real_height = backgroundImg.contentHeight;
			
			var w_percent:Number = real_width/this.width;
			var h_percent:Number = real_height/this.height;
			trace("<-----------------------------");
			trace(real_width+" "+real_height);
			trace(w_percent+" "+h_percent);
			trace(this.width+" "+this.height);
			if (w_percent <= 1 && h_percent <= 1 )
			{
				this.backgroundImg.height=real_height;
				this.backgroundImg.width=real_width;
				this.parentDocument.percent.text=100;
			}else{
				trace("else");
				if (h_percent > w_percent)
				{
					this.backgroundImg.height=this.height;
					this.backgroundImg.width=real_width/h_percent;
				}
				else
				{
					this.backgroundImg.width=this.width;
					this.backgroundImg.height=real_height/w_percent;
				}
				
			}
//			this.parentDocument.percent.text=Math.round(this.backgroundImg.width/real_width*100);
			trace(this.backgroundImg.width+" "+this.backgroundImg.height);
			trace("----------------------------->");
			
		}
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
//			formCanvas.x=backgroundImg.x;
//			formCanvas.y=backgroundImg.y;
//			if(backgroundImg.source!=null){
//				formCanvas.width=backgroundImg.width;
//				formCanvas.height=backgroundImg.height;
//			}else{
//				formCanvas.percentHeight=100;
//				formCanvas.percentWidth=100;
//			}
			formCanvas.width=this.width;
			formCanvas.height=this.height;
		}
		
		public function addField(field:FormItem):void{
			field.x=formCanvas.width/2;
			field.y=formCanvas.height/2;
			field.addEventListener(MouseEvent.MOUSE_DOWN,fieldMouseDownHandle);
			formCanvas.addChild(field);
			formCanvasModel.fields.addItem(field);
			items.addItem(field);
		}
		
		public function removeField(field:FormItem):void{
//			if(field==formCanvasModel.currentSelectedField){
//				
//			}
			field.removeEventListener(MouseEvent.MOUSE_DOWN,fieldMouseDownHandle);
			formCanvas.removeChild(field);
			formCanvasModel.fields.removeItemAt(formCanvasModel.fields.getItemIndex(field));
			items.removeItemAt(items.getItemIndex(field));
		}
		
		public function mouseMoveHandel(event:MouseEvent):void{
			var localPoint:Point=new Point();
			localPoint.x=event.stageX;
			localPoint.y=event.stageY;
			localPoint=formCanvas.globalToContent(localPoint);
//			Alert.show(mcx+" "+mcy+" "+localPoint.x+" "+localPoint.y);
			for(var obj:Object in formCanvasModel.fields.source){
				var field:FormItem=formCanvasModel.fields.getItemAt(obj as Number) as FormItem;
				if(formCanvasModel.currentSelectedField != field && field.isSelect){
					field.x=field.x+(localPoint.x-mcx-formCanvasModel.currentSelectedField.x);
					field.y=field.y+(localPoint.y-mcy-formCanvasModel.currentSelectedField.y);
				}
			}
			
//			formCanvasModel.currentSelectedField.x=localPoint.x-mcx;
//			formCanvasModel.currentSelectedField.y=localPoint.y-mcy;
			formCanvasModel.currentSelectedField.x=localPoint.x-lcx;
			formCanvasModel.currentSelectedField.y=localPoint.y-lcy;
			
		}
		
		private var mcx:Number;
		private var mcy:Number;
		private var lcx:Number;
		private var lcy:Number;
		
		public function mouseUp(event:MouseEvent):void{
			formCanvas.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandel);
			this.parent.removeEventListener(MouseEvent.MOUSE_UP,mouseUp);
		}
		
		public function fieldMouseDownHandle(event:MouseEvent):void{
			
			var currentTarget:FormItem = event.currentTarget as FormItem;
			this.parentDocument.toolPanel.atsItem=currentTarget;
			
			formCanvas.setChildIndex(currentTarget,formCanvas.numChildren-1);
			if(event.target == currentTarget){
				mcx=event.localX;
				mcy=event.localY;
			}else{
				var localPoint:Point=new Point();
				localPoint.x=event.stageX;
				localPoint.y=event.stageY;
				localPoint=currentTarget.globalToContent(localPoint);
				mcx=localPoint.x;
				mcy=localPoint.y;
				
				var canvasPoint:Point=new Point();
				canvasPoint.x=event.stageX;
				canvasPoint.y=event.stageY;
				canvasPoint=formCanvas.globalToContent(canvasPoint);
				lcx=canvasPoint.x-currentTarget.x;
				lcy=canvasPoint.y-currentTarget.y;;
				trace(canvasPoint.x+"  "+canvasPoint.y);
				trace(lcx+"  "+lcy);
				trace(currentTarget.x+"  "+currentTarget.y);
			}
			if(event.target is BorderPoint){
			}else{
				if(event.ctrlKey){
					if(currentTarget.isSelect){
						formCanvasModel.selectedIndex.removeItemAt(formCanvasModel.selectedIndex.getItemIndex(currentTarget));
						currentTarget.hideBorderPoint();
						currentTarget.hideBorder();
						if(formCanvasModel.selectedIndex.length>0){
							showMultipleSelectedBorder(formCanvasModel.selectedIndex.getItemAt(formCanvasModel.selectedIndex.length-1) as FormItem);
						}else{
							currentTarget.showBorder();
							currentTarget.showBorderPoint();
							formCanvasModel.selectedIndex.addItem(currentTarget);
						}
					}else{
						showMultipleSelectedBorder(currentTarget);
						formCanvasModel.selectedIndex.addItem(currentTarget);
					}
					
				}else{
					if(formCanvasModel.selectedIndex.length>1 && currentTarget.isSelect){
						showMultipleSelectedBorder(event.currentTarget as FormItem);
						formCanvasModel.isSelectedMultiple=false;
						currentTarget.addEventListener(MouseEvent.MOUSE_UP,fieldMouseUpHandle);
						currentTarget.addEventListener(MouseEvent.MOUSE_MOVE,fieldMouseMoveHandle);
					}else{
						formCanvasModel.selectedIndex.removeAll();
						formCanvasModel.selectedIndex.addItem(currentTarget);
						currentTarget.showBorder();
						currentTarget.showBorderPoint();
						for(var obj2:Object in formCanvasModel.fields.source){
							var field:FormItem=formCanvasModel.fields.getItemAt(obj2 as Number) as FormItem;
							if(field!=currentTarget){
								field.hideBorder();
								field.hideBorderPoint();
							}
						}
					}
				}
				formCanvas.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandel);
				this.parent.addEventListener(MouseEvent.MOUSE_UP,mouseUp);
			}
			formCanvasModel.currentSelectedField=currentTarget;
		}
		
		public function fieldMouseUpHandle(event:MouseEvent):void{
			formCanvasModel.currentSelectedField.removeEventListener(MouseEvent.MOUSE_UP,fieldMouseUpHandle);
			formCanvasModel.currentSelectedField.removeEventListener(MouseEvent.MOUSE_MOVE,fieldMouseMoveHandle);
			if(!formCanvasModel.isSelectedMultiple){
				formCanvasModel.selectedIndex.removeAll();
				formCanvasModel.selectedIndex.addItem(formCanvasModel.currentSelectedField);
				formCanvasModel.currentSelectedField.showBorder();
				formCanvasModel.currentSelectedField.showBorderPoint();
				for(var obj2:Object in formCanvasModel.fields.source){
					var field:FormItem=formCanvasModel.fields.getItemAt(obj2 as Number) as FormItem;
					if(field!=formCanvasModel.currentSelectedField){
						field.hideBorder();
						field.hideBorderPoint();
					}
				}
			}
		}
		public function fieldMouseMoveHandle(event:MouseEvent):void{
			formCanvasModel.isSelectedMultiple=true;
		}
		public function showMultipleSelectedBorder(formItem:FormItem):void{
			formItem.setStyle("borderSkin",com.ats.developer.components.borderpint.DashBorder);
			formItem.setStyle("borderThickness","2");
			formItem.setStyle("borderColor","#567843");
			formItem.setStyle("borderStyle","solid");
			formItem.isSelect=true;
			formCanvasModel.currentSelectedField=formItem;
			for(var obj:Object in formCanvasModel.fields.source){
				var selectedIndexItem:FormItem=formCanvasModel.fields.getItemAt(obj as Number) as FormItem;
				if(selectedIndexItem!=formItem && selectedIndexItem.isSelect){
					selectedIndexItem.showBorder();
					selectedIndexItem.hideBorderPoint();
				}
			}
		}
		
		public function mouseUpHandler(event:MouseEvent):void{
			formCanvas.removeEventListener(MouseEvent.MOUSE_MOVE ,doMove);
			box.visible=false;
			box.width=0;
			box.height=0;
			formCanvas.mouseChildren=true;
			this.parent.removeEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
		}
		
		public function mouseDownHandler(event:MouseEvent):void
		{
			if(!event.ctrlKey){
				//如果是画板
				if(event.currentTarget==event.target){
					this.parentDocument.toolPanel.disAbled();
					formCanvasModel.selectedIndex.removeAll();
					for(var obj:Object in formCanvasModel.fields.source){
						if((formCanvasModel.fields.getItemAt(obj as Number) as FormItem).isSelect){
							(formCanvasModel.fields.getItemAt(obj as Number) as FormItem).hideBorder();
							(formCanvasModel.fields.getItemAt(obj as Number) as FormItem).hideBorderPoint();
						}
					}
					var point2:Point=new Point(event.localX,event.localY);
					point2=formCanvas.localToContent(point2);
					xx=point2.x;
					yy=point2.y;
					box.x=point2.x;
					box.y=point2.y;
					box.width=0;
					box.height=0;
					box.visible=true;
					formCanvas.addEventListener(MouseEvent.MOUSE_MOVE,doMove);
					this.parent.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
				}
				
				//如果是边框点
				if(event.target is BorderPoint){
					var point:Point=new Point(event.stageX,event.stageY);
					point=formCanvas.globalToLocal(point);
					xx=point.x;
					yy=point.y;
					formCanvas.mouseChildren=false;
					if(event.target is RBorderPoint){
						formCanvas.addEventListener(MouseEvent.MOUSE_MOVE,rBorderPoint_MouseMoveHandler);
						formCanvas.addEventListener(MouseEvent.MOUSE_UP,rBorderPoint_MouseUpHandler);
					}
					if(event.target is LBorderPoint){
						formCanvas.addEventListener(MouseEvent.MOUSE_MOVE,lBorderPoint_MouseMoveHandler);
						formCanvas.addEventListener(MouseEvent.MOUSE_UP,lBorderPoint_MouseUpHandler);
					}
					if(event.target is BBorderPoint){
						formCanvas.addEventListener(MouseEvent.MOUSE_MOVE,bBorderPoint_MouseMoveHandler);
						formCanvas.addEventListener(MouseEvent.MOUSE_UP,bBorderPoint_MouseUpHandler);
					}
					if(event.target is TBorderPoint){
						formCanvas.addEventListener(MouseEvent.MOUSE_MOVE,tBorderPoint_MouseMoveHandler);
						formCanvas.addEventListener(MouseEvent.MOUSE_UP,tBorderPoint_MouseUpHandler);
					}
					if(event.target is TLBorderPoint){
						formCanvas.addEventListener(MouseEvent.MOUSE_MOVE,ltBorderPoint_MouseMoveHandler);
						formCanvas.addEventListener(MouseEvent.MOUSE_UP,ltBorderPoint_MouseUpHandler);
					}
					if(event.target is BRBorderPoint){
						formCanvas.addEventListener(MouseEvent.MOUSE_MOVE,brBorderPoint_MouseMoveHandler);
						formCanvas.addEventListener(MouseEvent.MOUSE_UP,brBorderPoint_MouseUpHandler);
					}
					if(event.target is LBBorderPoint){
						formCanvas.addEventListener(MouseEvent.MOUSE_MOVE,lbBorderPoint_MouseMoveHandler);
						formCanvas.addEventListener(MouseEvent.MOUSE_UP,lbBorderPoint_MouseUpHandler);
					}
					if(event.target is TRBorderPoint){
						formCanvas.addEventListener(MouseEvent.MOUSE_MOVE,rtBorderPoint_MouseMoveHandler);
						formCanvas.addEventListener(MouseEvent.MOUSE_UP,rtBorderPoint_MouseUpHandler);
					}
				}
			}
		}
		
		/*这里开始是边框点拖动事件处理*/
		/*右边框点*/
		public function rBorderPoint_MouseMoveHandler(event:MouseEvent):void{
			CursorManager.setCursor(HRESIZECURSOR,2,-10,-10);
			moveRight(event);
		}
		protected function moveRight(event:MouseEvent):void{
			if(event.localX > formCanvasModel.currentSelectedField.x){
				formCanvasModel.currentSelectedField.width=event.localX-formCanvasModel.currentSelectedField.x;
			}
		}
		public function rBorderPoint_MouseUpHandler(event:MouseEvent):void{
			formCanvas.mouseChildren=true;
			CursorManager.removeAllCursors();
			formCanvas.removeEventListener(MouseEvent.MOUSE_MOVE,rBorderPoint_MouseMoveHandler);
			formCanvas.removeEventListener(MouseEvent.MOUSE_UP,rBorderPoint_MouseUpHandler);
		}
		/*  End  */
		
		/*左边框点*/
		public function lBorderPoint_MouseMoveHandler(event:MouseEvent):void{
			CursorManager.setCursor(HRESIZECURSOR,2,-10,-10);
			moveLeft(event);
		}
		public function lBorderPoint_MouseUpHandler(event:MouseEvent):void{
			formCanvas.mouseChildren=true;
			CursorManager.removeAllCursors();
			formCanvas.removeEventListener(MouseEvent.MOUSE_MOVE,lBorderPoint_MouseMoveHandler);
			formCanvas.removeEventListener(MouseEvent.MOUSE_UP,lBorderPoint_MouseUpHandler);
		}
		protected function moveLeft(event:MouseEvent):void{
			if(event.localX < getLeftXPoint()){
				formCanvasModel.currentSelectedField.width=formCanvasModel.currentSelectedField.width-(event.localX-formCanvasModel.currentSelectedField.x);
				formCanvasModel.currentSelectedField.x=event.localX;
			}
			if(event.localX < formCanvasModel.currentSelectedField.x){
				formCanvasModel.currentSelectedField.width=formCanvasModel.currentSelectedField.width+(formCanvasModel.currentSelectedField.x-event.localX);
				formCanvasModel.currentSelectedField.x=event.localX;
			}
		}
		/*  End  */
		/*上边框点*/
		public function tBorderPoint_MouseMoveHandler(event:MouseEvent):void{
			CursorManager.setCursor(VRESIZECURSOR,2,-10,-10);
			moveTop(event);
		}
		protected function moveTop(event:MouseEvent):void{
			if(event.localY < getLeftYPoint()){
				formCanvasModel.currentSelectedField.height=formCanvasModel.currentSelectedField.height-(event.localY-formCanvasModel.currentSelectedField.y);
				formCanvasModel.currentSelectedField.y=event.localY;
			}
			if(event.localY < formCanvasModel.currentSelectedField.y){
				formCanvasModel.currentSelectedField.height=formCanvasModel.currentSelectedField.height+(formCanvasModel.currentSelectedField.y-event.localY);
				formCanvasModel.currentSelectedField.y=event.localY;
			}
		}
		public function tBorderPoint_MouseUpHandler(event:MouseEvent):void{
			formCanvas.mouseChildren=true;
			CursorManager.removeAllCursors();
			formCanvas.removeEventListener(MouseEvent.MOUSE_MOVE,tBorderPoint_MouseMoveHandler);
			formCanvas.removeEventListener(MouseEvent.MOUSE_UP,tBorderPoint_MouseUpHandler);
		}
		/*  End  */
		/*下边框点*/
		public function bBorderPoint_MouseMoveHandler(event:MouseEvent):void{
			CursorManager.setCursor(VRESIZECURSOR,2,-10,-10);
			moveBelow(event);
		}
		protected function moveBelow(event:MouseEvent):void{
			if(event.localY > formCanvasModel.currentSelectedField.y){
				formCanvasModel.currentSelectedField.height=event.localY-formCanvasModel.currentSelectedField.y;
			}
		}
		public function bBorderPoint_MouseUpHandler(event:MouseEvent):void{
			formCanvas.mouseChildren=true;
			CursorManager.removeAllCursors();
			formCanvas.removeEventListener(MouseEvent.MOUSE_MOVE,bBorderPoint_MouseMoveHandler);
			formCanvas.removeEventListener(MouseEvent.MOUSE_UP,bBorderPoint_MouseUpHandler);
		}
		/*  End  */
		/*左上边框点*/
		public function ltBorderPoint_MouseMoveHandler(event:MouseEvent):void{
			CursorManager.setCursor(TLBRRESIZECURSOR,2,-10,-10);
			moveTop(event);
			moveLeft(event);
		}
		public function ltBorderPoint_MouseUpHandler(event:MouseEvent):void{
			formCanvas.mouseChildren=true;
			CursorManager.removeAllCursors();
			formCanvas.removeEventListener(MouseEvent.MOUSE_MOVE,ltBorderPoint_MouseMoveHandler);
			formCanvas.removeEventListener(MouseEvent.MOUSE_UP,ltBorderPoint_MouseUpHandler);
		}
		/*  End  */
		/*右下边框点*/
		public function brBorderPoint_MouseMoveHandler(event:MouseEvent):void{
			CursorManager.setCursor(TLBRRESIZECURSOR,2,-10,-10);
			moveRight(event);
			moveBelow(event);
		}
		public function brBorderPoint_MouseUpHandler(event:MouseEvent):void{
			formCanvas.mouseChildren=true;
			CursorManager.removeAllCursors();
			formCanvas.removeEventListener(MouseEvent.MOUSE_MOVE,brBorderPoint_MouseMoveHandler);
			formCanvas.removeEventListener(MouseEvent.MOUSE_UP,brBorderPoint_MouseUpHandler);
		}
		/*  End  */
		/*左下边框点*/
		public function lbBorderPoint_MouseMoveHandler(event:MouseEvent):void{
			CursorManager.setCursor(TRBLRESIZECURSOR,2,-10,-10);
			moveLeft(event);
			moveBelow(event);
		}
		public function lbBorderPoint_MouseUpHandler(event:MouseEvent):void{
			formCanvas.mouseChildren=true;
			CursorManager.removeAllCursors();
			formCanvas.removeEventListener(MouseEvent.MOUSE_MOVE,lbBorderPoint_MouseMoveHandler);
			formCanvas.removeEventListener(MouseEvent.MOUSE_UP,lbBorderPoint_MouseUpHandler);
		}
		/*  End  */
		/*右上边框点*/
		public function rtBorderPoint_MouseMoveHandler(event:MouseEvent):void{
			CursorManager.setCursor(TRBLRESIZECURSOR,2,-10,-10);
			moveTop(event);
			moveRight(event);
		}
		public function rtBorderPoint_MouseUpHandler(event:MouseEvent):void{
			formCanvas.mouseChildren=true;
			CursorManager.removeAllCursors();
			formCanvas.removeEventListener(MouseEvent.MOUSE_MOVE,rtBorderPoint_MouseMoveHandler);
			formCanvas.removeEventListener(MouseEvent.MOUSE_UP,rtBorderPoint_MouseUpHandler);
		}
		/*  End  */
		
		/*这里结束边框点拖动事件处理*/
		
		public var xx:Number;
		public var yy:Number;
		
		protected function doMove(event:MouseEvent):void{
			formCanvas.mouseChildren=false;
			var point2:Point=new Point(event.localX,event.localY);
			point2=formCanvas.localToContent(point2);
			box.width=point2.x-xx;
			box.height=point2.y-yy;
			
			for(var obj:Object in formCanvasModel.fields.source){
				var field:FormItem=formCanvasModel.fields.getItemAt(obj as Number) as FormItem;
				if(regionSelect(field)!=null){
					if(formCanvasModel.selectedIndex.getItemIndex(field)<0){
						if(formCanvasModel.selectedIndex.length>0){
							showMultipleSelectedBorder(field);
						}else{
							field.showBorder();
							field.showBorderPoint();
						}
						formCanvasModel.selectedIndex.addItem(field);
					}
					formCanvasModel.currentSelectedField=field;
				}else{
					field.hideBorder();
					field.hideBorderPoint();
					if(formCanvasModel.selectedIndex.getItemIndex(field)>=0){
						formCanvasModel.selectedIndex.removeItemAt(formCanvasModel.selectedIndex.getItemIndex(field));
						if(formCanvasModel.selectedIndex.length>0){
							showMultipleSelectedBorder(formCanvasModel.selectedIndex.getItemAt(formCanvasModel.selectedIndex.length-1) as FormItem);
						}
					}
					//					formCanvasModel.currentSelectedField=formCanvasModel.selectedIndex.getItemAt(formCanvasModel.selectedIndex.length-1) as FormItem;
				}
			}
		}
		
		protected function regionSelect(field:FormItem):FormItem{
			var s_top:Number; 
			var s_left:Number; 
			
			if(box.height < 0){
				s_top = box.y+box.height; 
			}else{
				s_top = box.y;
			}
			if(box.width < 0){
				s_left = box.x+box.width;
			}else{
				s_left = box.x;
			}
			var s_right:Number = s_left + Math.abs(box.width); 
			var s_bottom:Number = s_top + Math.abs(box.height); 
			
			var r_top:Number = field.y; 
			var r_left:Number = field.x; 
			var r_right:Number = r_left + field.width; 
			var r_bottom:Number = r_top + field.height; 
			
			var t:Number = Math.max(s_top, r_top); 
			var r:Number = Math.min(s_right, r_right); 
			var b:Number = Math.min(s_bottom, r_bottom); 
			var l:Number = Math.max(s_left, r_left); 
			
			if (b > t+5 && r > l+5) { 
				return field; 
			} else { 
				return null;
			} 
		}
		
		protected function getLeftXPoint():Number{
			return formCanvasModel.currentSelectedField.x+formCanvasModel.currentSelectedField.width;
		}
		protected function getLeftYPoint():Number{
			return formCanvasModel.currentSelectedField.y+formCanvasModel.currentSelectedField.height;
		}
		
		[Embed(source="/assets/img/resizeCursorH.gif")]
		protected var HRESIZECURSOR:Class;
		[Embed(source="/assets/img/resizeCursorV.gif")]
		protected var VRESIZECURSOR:Class;
		[Embed(source="/assets/img/resizeCursorTLBR.gif")]
		protected var TLBRRESIZECURSOR:Class;
		[Embed(source="/assets/img/resizeCursorTRBL.gif")]
		protected var TRBLRESIZECURSOR:Class;
		
		protected function formCanvas_mouseOverHandler(event:MouseEvent):void
		{
			if(event.target is LBorderPoint || event.target is RBorderPoint){
				CursorManager.setCursor(HRESIZECURSOR,2,-10,-10);
				(event.target as UIComponent).addEventListener(MouseEvent.MOUSE_OUT,borderPointMouseOutHandler);
			}
			if(event.target is TBorderPoint || event.target is BBorderPoint){
				CursorManager.setCursor(VRESIZECURSOR,2,-10,-10);
				(event.target as UIComponent).addEventListener(MouseEvent.MOUSE_OUT,borderPointMouseOutHandler);
			}
			if(event.target is TLBorderPoint || event.target is BRBorderPoint){
				CursorManager.setCursor(TLBRRESIZECURSOR,2,-10,-10);
				(event.target as UIComponent).addEventListener(MouseEvent.MOUSE_OUT,borderPointMouseOutHandler);
			}
			if(event.target is LBBorderPoint || event.target is TRBorderPoint){
				CursorManager.setCursor(TRBLRESIZECURSOR,2,-10,-10);
				(event.target as UIComponent).addEventListener(MouseEvent.MOUSE_OUT,borderPointMouseOutHandler);
			}
		}
		protected function borderPointMouseOutHandler(evnet:MouseEvent):void{
			CursorManager.removeAllCursors();
		}
		protected function clickHandler( event : MouseEvent ) : void {
			this.stage.focus = this;
		}
		protected function keyDownHandle(event:KeyboardEvent):void{
			var field:FormItem;
			if(event.shiftKey){
				if(event.keyCode==37){
					for each(field in formCanvasModel.fields){
						if(field.isSelect){
							field.x=field.x-1;
						}
					}
				}
				if(event.keyCode==38){
					for each(field in formCanvasModel.fields){
						if(field.isSelect){
							field.y=field.y-1;
						}
					}
				}
				if(event.keyCode==39){
					for each(field in formCanvasModel.fields){
						if(field.isSelect){
							field.x=field.x+1;
						}
					}
				}
				if(event.keyCode==40){
					for each(field in formCanvasModel.fields){
						if(field.isSelect){
							field.y=field.y+1;
						}
					}
				}
			}else{
				if(event.keyCode==37){
					for each(field in formCanvasModel.fields){
						if(field.isSelect){
							field.x=field.x-10;
						}
					}
				}
				if(event.keyCode==38){
					for each(field in formCanvasModel.fields){
						if(field.isSelect){
							field.y=field.y-10;
						}
					}
				}
				if(event.keyCode==39){
					for each(field in formCanvasModel.fields){
						if(field.isSelect){
							field.x=field.x+10;
						}
					}
				}
				if(event.keyCode==40){
					for each(field in formCanvasModel.fields){
						if(field.isSelect){
							field.y=field.y+10;
						}
					}
				}
				if(event.keyCode==46){
					if(formCanvasModel.currentSelectedField!=null){
						this.removeField(formCanvasModel.currentSelectedField);
					}
				}
				
			}
		}
	}
	
}
